<html>
<head>
  <meta charset="UTF-8">
  <title>管理员列表</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta http-equiv="Access-Control-Allow-Origin" content="*">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <link rel="stylesheet" href="/static/layui/css/layui.css" media="all">
</head>
<body>

<table class="layui-hide" id="demo" lay-filter="test"></table>
<!-- 自定义的控件 -->
<div id="toolbar" style="display: none; ">
  <div class="layui-btn-group">
    <button class="layui-btn layui-btn-sm" lay-event="edit" data-method="notice" data-type="auto">添加新角色</button>
  </div>
</div>
<script src="/static/js/jquery-3.3.1.js"></script>
<script src="/static/layui/layui.js"></script>
<script>
  layui.use(['table','layer','jquery'], function(){
    var table = layui.table;
    var  layer = layui.layer;
    var  $ = layui.jquery;
    //第一个实例
    table.render({
      elem: '#demo'
      ,height: document.body.clientHeight-180
      ,url: '/user/addfind' //数据接口
      ,page:true
      ,id:'testReload'
      ,cols: [[ //表头
        {field: 'email', title: 'email', sort: true}
        ,{field: 'username', title: '姓名', sort: true}
        ,{field: 'description', title: '已拥有角色', sort: true
          /*,templet: function (d) {
            return d.his_role.description;}*/
      }
      ]]
      ,toolbar:'#toolbar',//表格头信息
      parseData: function (rs) {//数据格式解析
        console.log(rs);
        if (rs.code == 200) {
          return {
            "code": rs.code,
            "msg": rs.msg,
            "count": rs.date.length,
            "data": rs.date
          }
        }
      },
      response: {	// 设置响应码
        statusCode: 200 //规定成功的状态码，默认：0
      }
    });

    //监听事件
    table.on('toolbar(test)', function (obj) {
      var checkStatus = table.checkStatus(obj.config.id);
      switch (obj.event) {
        case 'edit':
          open("添加新角色","/view/hisuser/add.html");
          break;
      };
    });
    //"用户添加页面",
    function open(title,url){
      layer.open({
        type: 2
        ,title: title
        ,content:url
        ,offset: '50px'
        ,area: ['500px', '60%']
        ,end:function () {
          table.reload('testReload');
        }
      });
    }

    function searchBtn(){

    }

    var $ = layui.$, active = {
      reload: function () {
        var l_name = $('#l_name');
        //执行重载
        table.reload('testReload', {
          page: {
            curr: 1 //重新从第 1 页开始
          }
          , where: {
            "l_name": l_name.val()
          }
        });
      }
    };
    $('.demoTable .layui-btn').on('click', function () {
      var type = $(this).data('type');
      active[type] ? active[type].call(this) : '';
    });
  });
</script>
</body>
</html>